package com.zznj.xzjc.activity;

import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.zznj.xzjc.R;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.net.StatusBarUtil;
import com.zznj.xzjc.utils.NetWorkUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 单个视频播放
 */
public class VedioMsgActivity extends BaseActivity {
    //返回
    @BindView(R.id.iv_back1)
    ImageView iv_back1;
    //视频
    @BindView(R.id.video_view)
    SampleCoverVideo videoView;
    //播放图标
    @BindView(R.id.img_play)
    ImageView imgPlay;

    private String videoPath;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_vedio_msg_layout;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setDarkMode(this);
        //不锁屏幕
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        videoPath = getIntent().getExtras().getString("videoPath");
    }

    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    Dialog mCommentDialog;
    private SmartRefreshLayout mSrlComment;
    private EditText mEtInput;
    private TextView mTvSendMessage;
    private RecyclerView mRvComment;
    private ImageView mIvClose, mIvNone;
    private LinearLayout ll_huifu;

    @Override
    protected void initData() {
        playVideo();
    }

    private void playVideo() {
        videoView.loadCoverImage(videoPath, R.drawable.shape_black);
        videoView.setUpLazy(videoPath, true, null, null, "这是title");
        //增加title
        videoView.getTitleTextView().setVisibility(View.GONE);
        //设置返回键
        videoView.getBackButton().setVisibility(View.GONE);
        //设置全屏按键功能
        videoView.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                videoView.startWindowFullscreen(mContext, false, true);
            }
        });
        //防止错位设置
        videoView.setPlayTag("TAG");
        videoView.setLockLand(true);
        videoView.setPlayPosition(0);
        //是否根据视频尺寸，自动选择竖屏全屏或者横屏全屏，这个标志为和 setLockLand 冲突，需要和 orientationUtils 使用
        videoView.setAutoFullWithSize(false);
        //音频焦点冲突时是否释放
        videoView.setReleaseWhenLossAudio(false);
        //全屏动画
        videoView.setShowFullAnimation(true);
        //小屏时不触摸滑动
        videoView.setIsTouchWiget(false);
        videoView.requestFocus();
        doPlay();
    }

    private void doPlay() {
        //在无线网下自己播放
        if (NetWorkUtil.getAPNType(this) == ConnectivityManager.TYPE_WIFI) {
            startVideoPlay();
        } else {
            startVideoPlay();
        }
    }

    private void startVideoPlay() {
        if (null == videoView) return;
        videoView.startPlayLogic();
    }


    @OnClick({R.id.iv_back1})
    public void onViewClicked(View v) {
        if (v.getId() == R.id.iv_back1) {
            //返回
            finish();
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        if (null != videoView) {
            videoView.release();
        }
        super.onDestroy();
    }
}

